<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Productprint extends CI_Controller
{

    /**
     * 印花订单
     *
     * Maps to the following URL
     *        http://example.com/index.php/welcome
     *    - or -
     *        http://example.com/index.php/welcome/index
     *    - or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see https://codeigniter.com/user_guide/general/urls.html
     */
    function __construct()
    {
        parent::__construct();
        $this->load->model('Function_m','fun');
        $this->pageSize =$this->config->item('pagenum');
    }
    public function showlist() {

        $factorys = $this->config->item('factorys');
        $factorys = explode("|",$factorys);
        $this->tpl->assign("factorys",$factorys);
        $nowpage = $_REQUEST["pageid"];
        if ($nowpage == "") $nowpage = 1;
        $where = "";

        if($_COOKIE["classid"]==12)
        {
            $where.=" AND a.fid =1";
        }
        if($_COOKIE["classid"]==13)
        {
            $where.=" AND a.fid =2";
        }

        //商品编号，创建人，序号
        $search_goods = trim($this->input->get('search_goods'));
        if($search_goods){
            $this->tpl->assign("search_goods", $search_goods);
            $goods = trim($this->input->get('goods'));
            if($goods){
                $this->tpl->assign("goods", $goods);
                if($search_goods == 1){
                    $where .= " AND a.goods_code = '$goods' ";
                }elseif ($search_goods==2){
                    $where .= " AND a.create_user = '$goods' ";
                }elseif($search_goods==3){
                    $where .= " AND a.id = '$goods' ";
                }else{
                }
            }
        }else{
            $goods = trim($this->input->get('goods'));
            if($goods){
                $this->tpl->assign("goods", $goods);
                $where.= " AND (a.goods_code = '{$goods}' or a.id = '{$goods}') ";
            }
        }
        //状态
        $search_status = trim($this->input->get('search_status'));
        if($search_status){
            $this->tpl->assign("search_status", $search_status);
            $where .= " AND a.status = $search_status ";
        }else{
            $where .= " AND a.status != 3 ";
        }
        //外发工厂
        $factorys = trim($this->input->get('factorys'));
        if($factorys){
            $this->tpl->assign("factory", $factorys);
            $where .= " AND a.factory = '$factorys' ";
        }
        //查询日期
        $search_date = $this->input->get('search_date');
        if($search_date){
            $this->tpl->assign("search_date", $search_date);
            //日期
            if($search_date==1){
                $starttime = trim($this->input->get('starttime'));
                if($starttime){
                    $this->tpl->assign("starttime", $starttime);
                    $where .= " AND a.createtime >= '$starttime' ";
                }
                $endtime = trim($this->input->get('endtime'));
                if($endtime){
                    $this->tpl->assign("endtime", $endtime);
                    $endtime = $endtime." 23:59:59";
                    $where .= " AND a.createtime <= '$endtime' ";
                }
            }
            //日期
            if($search_date==2){
                $starttime = trim($this->input->get('starttime'));
                if($starttime){
                    $this->tpl->assign("starttime", $starttime);
                    $where .= " AND a.finishtime >= '$starttime' ";
                }
                $endtime = trim($this->input->get('endtime'));
                if($endtime){
                    $this->tpl->assign("endtime", $endtime);
                    $endtime = $endtime." 23:59:59";
                    $where .= " AND a.finishtime <= '$endtime' ";
                }
            }

        }else{
            //默认情况下，以创建日期来进行搜索
            $starttime = trim($this->input->get('starttime'));
            if($starttime){
                $this->tpl->assign("starttime", $starttime);
                $where .= " AND a.createtime >= '$starttime' ";
            }
            $endtime = trim($this->input->get('endtime'));
            if($endtime){
                $this->tpl->assign("endtime", $endtime);
                $endtime = $endtime." 23:59:59";
                $where .= " AND a.createtime <= '$endtime' ";
            }
        }
        $orderby = " order by a.id DESC ";
        $where.= " GROUP BY a.id ";

        $flag_excel = $this->input->get("flag_excel");
        if($flag_excel == 1){
            require dirname(__FILE__)."/../../fuman/ClassesExcel/PHPExcel.php";
            $objExcel = new PHPExcel();
            $this->pageSize = 1000000;
            $list = $this->fun->getalllist('db_product_print a', $where, $nowpage, $if_csv, $this->pageSize, $orderby,$fields = "a.*",$join);
            foreach($list['list'] as $kkk=>$vvv){
                $sql = "SELECT * FROM db_goods a LEFT JOIN db_goods_images b ON a.goods_id = b.goods_id WHERE a.goods_sku = '{$vvv['goods_code']}' GROUP BY a.goods_id  ";
                $rs  = $this->db->query($sql)->row_array();
                $list['list'][$kkk]['thumb_url'] = $rs['thumb_url'];
                $list['list'][$kkk]['local_url'] = $rs['local_url'];

            }
            $objExcel->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objExcel->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);

            //自定义 长 宽
            $objExcel->setActiveSheetIndex(0);
            $objExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight();
            $objExcel->getActiveSheet()->getColumnDimension('A')->setWidth();
            $objExcel->getActiveSheet()->getColumnDimension('B')->setWidth();
            $objExcel->getActiveSheet()->getColumnDimension('C')->setWidth(36);
            $objExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
            $objExcel->getActiveSheet()->getColumnDimension('E')->setWidth();
            $objExcel->getActiveSheet()->getColumnDimension('F')->setWidth();
            $objExcel->getActiveSheet()->getColumnDimension('G')->setWidth();
            $objExcel->getActiveSheet()->getColumnDimension('H')->setWidth();
            $objExcel->getActiveSheet()->getColumnDimension('I')->setWidth();
            $objExcel->getActiveSheet()->getColumnDimension('J')->setWidth();
            $objExcel->getActiveSheet()->getColumnDimension('K')->setWidth();
            $objExcel->getActiveSheet()->getColumnDimension('L')->setWidth();
            $objExcel->getActiveSheet()->getColumnDimension('M')->setWidth(20);
            $objExcel->getActiveSheet()->getColumnDimension('N')->setWidth();

            //设置标题
            $objExcel->setActiveSheetIndex(0)->setCellValue('A1','序号');
            $objExcel->setActiveSheetIndex(0)->setCellValue('B1','商品编号');
            $objExcel->setActiveSheetIndex(0)->setCellValue('C1','商品名称');
            $objExcel->setActiveSheetIndex(0)->setCellValue('D1','工厂');
            $objExcel->setActiveSheetIndex(0)->setCellValue('E1','状态');
            $objExcel->setActiveSheetIndex(0)->setCellValue('F1','印花数量');
            $objExcel->setActiveSheetIndex(0)->setCellValue('G1','绣花数量');
            $objExcel->setActiveSheetIndex(0)->setCellValue('H1','印花价格');
            $objExcel->setActiveSheetIndex(0)->setCellValue('I1','绣花价格');
            $objExcel->setActiveSheetIndex(0)->setCellValue('J1','主料价格');
            $objExcel->setActiveSheetIndex(0)->setCellValue('K1','辅料价格');
            $objExcel->setActiveSheetIndex(0)->setCellValue('L1','完成工期');
            $objExcel->setActiveSheetIndex(0)->setCellValue('M1','创建时间');
            $objExcel->setActiveSheetIndex(0)->setCellValue('N1','创建人');
            foreach($list['list'] as $k=>$v){

                $count = $k+2;
                $objExcel->setActiveSheetIndex(0)->setCellValue('A'.$count,$v['id']);
                $objExcel->setActiveSheetIndex(0)->setCellValue('B'.$count,$v['goods_code']);
                $objExcel->setActiveSheetIndex(0)->setCellValue('C'.$count,$v['goods_name']);
                if($v['status']==1){
                    $status = "生产中";
                }
                if($v['status']==2){
                    $status = "已完成";
                }
                if($v['status']==3){
                    $status = "已作废";
                }
                if($v['status']==4){
                    $status = "新订单";
                }
                $objExcel->setActiveSheetIndex(0)->setCellValue('D'.$count,$v['factory']);
                $objExcel->setActiveSheetIndex(0)->setCellValue('E'.$count,$status);

                $objExcel->setActiveSheetIndex(0)->setCellValue('F'.$count,$v['print_stock']);
                $objExcel->getActiveSheet()->setCellValueExplicit('F'.$count,$v['print_stock'],PHPExcel_Cell_DataType::TYPE_STRING);

                $objExcel->setActiveSheetIndex(0)->setCellValue('G'.$count,$v['embroider_stock']);
                $objExcel->getActiveSheet()->setCellValueExplicit('G'.$count,$v['embroider_stock'],PHPExcel_Cell_DataType::TYPE_STRING);

                $objExcel->setActiveSheetIndex(0)->setCellValue('H'.$count,$v['print_price']);
                $objExcel->getActiveSheet()->setCellValueExplicit('H'.$count,$v['print_price'],PHPExcel_Cell_DataType::TYPE_STRING);

                $objExcel->setActiveSheetIndex(0)->setCellValue('I'.$count,$v['embroider_price']);
                $objExcel->getActiveSheet()->setCellValueExplicit('I'.$count,$v['embroider_price'],PHPExcel_Cell_DataType::TYPE_STRING);

                $objExcel->setActiveSheetIndex(0)->setCellValue('J'.$count,$v['master_price']);
                $objExcel->getActiveSheet()->setCellValueExplicit('J'.$count,$v['master_price'],PHPExcel_Cell_DataType::TYPE_STRING);

                $objExcel->setActiveSheetIndex(0)->setCellValue('K'.$count,$v['lesser_price']);
                $objExcel->getActiveSheet()->setCellValueExplicit('K'.$count,$v['lesser_price'],PHPExcel_Cell_DataType::TYPE_STRING);

                $objExcel->setActiveSheetIndex(0)->setCellValue('L'.$count,$v['projecttime']);
                $objExcel->getActiveSheet()->setCellValueExplicit('L'.$count,$v['projecttime'],PHPExcel_Cell_DataType::TYPE_STRING);

                $objExcel->setActiveSheetIndex(0)->setCellValue('M'.$count,$v['createtime']);
                $objExcel->setActiveSheetIndex(0)->setCellValue('N'.$count,$v['create_user']);
            }
            $excel_name = date("YmdHis");
            header("Content-type:application/vnd.ms-excel;charset=gb2312");
            header('Content-Disposition: attachment;filename="'.$excel_name.'.xls"');
            header('Cache-Control: max-age=0');
            $objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
            $objWriter->save('php://output');
            exit;
        }

        $list = $this->fun->getalllist('db_product_print a', $where, $nowpage, $if_csv, $this->pageSize, $orderby,$fields = "a.*",$join);
        foreach($list['list'] as $kkk=>$vvv){
            $sql = "SELECT * FROM db_goods a LEFT JOIN db_goods_images b ON a.goods_id = b.goods_id WHERE a.goods_sku = '{$vvv['goods_code']}' GROUP BY a.goods_id  ";
            $rs  = $this->db->query($sql)->row_array();
            $list['list'][$kkk]['thumb_url'] = $rs['thumb_url'];
            $list['list'][$kkk]['local_url'] = $rs['local_url'];
        }
        $pageList = $this->pagenav->page($list["total"], $nowpage, $this->pageSize);
        $this->tpl->assign("totalpage", ceil($list["total"] / $this->pageSize));
        $this->tpl->assign("total", $list["total"]);
        $this->tpl->assign("pageList", $pageList);
        $this->tpl->assign("orderlist", $list["list"]);
        $this ->tpl->display("factory/productprintlist.tpl");
    }
    public function addproductprint(){

        if($this->input->post()){
            $post_str = $this->input->post('param');
            $post_arr = explode("&",$post_str);
            foreach($post_arr as $k=>$v){
                $value = explode("=",$v);
                $ary_data[$value[0]] = htmlspecialchars(urldecode($value[1]));
            }
            $ary_data['goods_sku'] = trim($ary_data['goods_sku']);
            $sql = "SELECT goods_id,goods_sku,goods_name FROM db_goods WHERE goods_sku = '{$ary_data['goods_sku']}' ";
            $result = $this->db->query($sql)->row_array();
            if(!$result){
                $arr_return = array('code'=>0,'msg'=>"该商品编号不存在，请重新输入");
                echo json_encode($arr_return);exit;
            }
            if(!$ary_data['goods_name']){
                $arr_return = array('code'=>0,'msg'=>"商品名称不能为空，请重新输入");
                echo json_encode($arr_return);exit;
            }
            if(!$ary_data['factorys']){
                $arr_return = array('code'=>0,'msg'=>"外发工厂必须选择一个");
                echo json_encode($arr_return);exit;
            }
            if($ary_data['print_number']>0){
                $ary_data['print_number'] = (int)$ary_data['print_number'];
            }else{
                //$arr_return = array('code'=>0,'msg'=>"印花数量必须为一个整数");
                //echo json_encode($arr_return);exit;
            }
            if($ary_data['finish_day']>0){
                $ary_data['finish_day'] = (int)$ary_data['finish_day'];
            }else{
                $arr_return = array('code'=>0,'msg'=>"完成工期必须为一个整数");
                echo json_encode($arr_return);exit;
            }
            if($_COOKIE["classid"]==12)
            {
                $arr_data['fid'] = 1;//所属工厂
            }
            if($_COOKIE["classid"]==13)
            {
                $arr_data['fid'] = 2;//所属工厂
            }
            $arr_data['goods_name'] = $ary_data['goods_name'];//商品名称
            $arr_data['factory'] = $ary_data['factorys'];//工厂
            $arr_data['print_stock'] = $ary_data['print_number'];//印花数
            $arr_data['embroider_stock'] = $ary_data['embroider_number'];//绣花数
            $arr_data['print_price'] = $ary_data['print_price'];//印花价格
            $arr_data['embroider_price'] = $ary_data['embroider_price'];//绣花价格
            $arr_data['master_price'] = $ary_data['master_price'];//主料价格
            $arr_data['lesser_price'] = $ary_data['lesser_price'];//辅料价格
            $arr_data['projecttime'] = $ary_data['finish_day'];//工期
            $arr_data['img_thumb_url'] = $ary_data['thumb_imgs'];//缩略图
            $arr_data['memo'] = $ary_data['memo'];//备注信息
            $arr_data['createtime'] = date("Y-m-d H:i:s");
            $arr_data['goods_code'] = $ary_data['goods_sku']; //商品编号
            $arr_data['create_user'] = $_COOKIE['username'];
            $arr_data['userid'] = $_COOKIE['userid'];
            $this->db->insert('db_product_print',$arr_data);
            if($this->db->insert_id()){
                //添加一条日志信息
                $arr_log = array();
                $arr_log['pid'] = $this->db->insert_id();
                $arr_log['updatetime'] = date("Y-m-d H:i:s");
                $arr_log['updateoper'] = $_COOKIE['userid'];
                $arr_log['descr'] = "状态由新订单自动修改为,生产中;";
                $arr_log['status'] = 1;
                $this->db->insert('db_product_printlog',$arr_log);

                $arr_return = array('code'=>1,'msg'=>'添加成功','data'=>"");
            }else{
                $arr_return = array('code'=>0,'msg'=>'添加失败','data'=>"");
            }
            echo json_encode($arr_return);exit;
        }else{
            $factorys = $this->config->item('factorys');
            $factorys = explode("|",$factorys);
            $this->tpl->assign("factorys",$factorys);
            $this->tpl->display('factory/productprintadd.tpl');
        }


    }
    public function editproductprint(){

        if($this->input->post()){
            $post_str = $this->input->post('param');
            $post_arr = explode("&",$post_str);
            foreach($post_arr as $k=>$v){
                $value = explode("=",$v);
                $ary_data[$value[0]] = htmlspecialchars(urldecode($value[1]));
            }

            $ary_data['goods_sku'] = trim($ary_data['goods_sku']);
            $sql = "SELECT goods_id,goods_sku,goods_name FROM db_goods WHERE goods_sku = '{$ary_data['goods_sku']}' ";
            $result = $this->db->query($sql)->row_array();
            if(!$result){
                $arr_return = array('code'=>0,'msg'=>"该商品编号不存在，请重新输入");
                echo json_encode($arr_return);exit;
            }
            if(!$ary_data['goods_name']){
                $arr_return = array('code'=>0,'msg'=>"商品名称不能为空，请输入");
                echo json_encode($arr_return);exit;
            }
            if(!$ary_data['factorys']){
                $arr_return = array('code'=>0,'msg'=>"外发工厂必须选择一个");
                echo json_encode($arr_return);exit;
            }
            if($ary_data['print_number']>0){
                $ary_data['print_number'] = (int)$ary_data['print_number'];
            }else{
                //$arr_return = array('code'=>0,'msg'=>"印花数量必须为一个整数");
                //echo json_encode($arr_return);exit;
            }
            if($ary_data['finish_day']>0){
                $ary_data['finish_day'] = (int)$ary_data['finish_day'];
            }else{
                $arr_return = array('code'=>0,'msg'=>"完成工期必须为一个整数");
                echo json_encode($arr_return);exit;
            }
            //$arr_data['goods_name'] = $ary_data['goods_name'];//商品名称


            $id = trim($ary_data['id']);

            //生产中
            $sql_print = "SELECT * FROM db_product_print WHERE (status = 1 or status = 2) AND id = '".$id."'";
            $rs_print  = $this->db->query($sql_print)->row_array();
            if(!$rs_print){
                $arr_return = array('code'=>0,'msg'=>"无法更新,该订单数据不存在或已作废");
                echo json_encode($arr_return);exit;
            }
            $message = "";

            $arr_data['factory'] = $ary_data['factorys'];//工厂
            if($ary_data['factorys'] != $rs_print['factory']){
                $message.= "外发工厂由：".$rs_print['factory'].",修改为,".$ary_data['factorys']."<br/>";
            }
            $arr_data['print_stock'] = $ary_data['print_number'];//印花数
            if($ary_data['print_number'] != $rs_print['print_stock']){
                $message.= "印花数量由：".$rs_print['print_stock'].",修改为,".$ary_data['print_number']."<br/>";
            }
            $arr_data['embroider_stock'] = $ary_data['embroider_number'];//绣花数
            if($ary_data['embroider_number'] != $rs_print['embroider_stock']){
                $message.= "绣花数量由：".$rs_print['embroider_stock'].",修改为,".$ary_data['embroider_number']."<br/>";
            }
            $arr_data['print_price'] = $ary_data['print_price'];//印花价格
            if($ary_data['print_price'] != $rs_print['print_price']){
                $message.= "印花价格由：".$rs_print['print_price'].",修改为,".$ary_data['print_price']."<br/>";
            }

            $arr_data['embroider_price'] = $ary_data['embroider_price'];//绣花价格
            if($ary_data['embroider_price'] != $rs_print['embroider_price']){
                $message.= "绣花价格由：".$rs_print['embroider_price'].",修改为,".$ary_data['embroider_price']."<br/>";
            }
            $arr_data['master_price'] = $ary_data['master_price'];//主料价格
            if($ary_data['master_price'] != $rs_print['master_price']){
                $message.= "主料价格由：".$rs_print['master_price'].",修改为,".$ary_data['master_price']."<br/>";
            }
            $arr_data['lesser_price'] = $ary_data['lesser_price'];//辅料价格
            if($ary_data['lesser_price'] != $rs_print['lesser_price']){
                $message.= "辅料价格由：".$rs_print['lesser_price'].",修改为,".$ary_data['lesser_price']."<br/>";
            }
            $arr_data['projecttime'] = $ary_data['finish_day'];//工期
            if($ary_data['finish_day'] != $rs_print['projecttime']){
                $message.= "完成工期由：".$rs_print['projecttime'].",修改为,".$ary_data['finish_day']."<br/>";
            }
            $arr_data['memo'] = $ary_data['memo'];//备注信息
            if($ary_data['memo'] != $rs_print['memo']){
                $message.= "备注由：".$rs_print['memo'].",修改为,".$ary_data['memo']."<br/>";
            }

            //$arr_data['createtime'] = date("Y-m-d H:i:s");
            //$arr_data['goods_code'] = $ary_data['goods_sku']; //商品编号
            $arr_data['create_user'] = $_COOKIE['username'];
            $arr_data['userid'] = $_COOKIE['userid'];

            $this->db->update('db_product_print',$arr_data,"id = '".$id."'");
            if($this->db->affected_rows()){
                //添加一条日志信息
                if($message){
                    $arr_log = array();
                    $arr_log['pid'] = $rs_print['id'];
                    $arr_log['updatetime'] = date("Y-m-d H:i:s");
                    $arr_log['updateoper'] = $_COOKIE['userid'];
                    $arr_log['descr'] = $message;
                    $arr_log['status'] = NULL;
                    $this->db->insert('db_product_printlog',$arr_log);
                }
                $arr_return = array('code'=>1,'msg'=>'更新成功','data'=>"");
            }else{
                $arr_return = array('code'=>0,'msg'=>'更新失败','data'=>"");
            }
            echo json_encode($arr_return);exit;
        }else{
            $id = $this->input->get('id');
            $sql = "SELECT a.*,c.local_url FROM db_product_print a LEFT JOIN db_goods b ON a.goods_code = b.goods_sku LEFT JOIN db_goods_images c ON b.goods_id = c.goods_id WHERE a.id = '{$id}' GROUP BY a.id ";
            $rs  = $this->db->query($sql)->row_array();
            $this->tpl->assign('goods',$rs);

            $sql_log = "SELECT a.id,a.updatetime,a.descr,b.name as updateoper FROM db_product_printlog a LEFT JOIN d_user b ON a.updateoper = b.id WHERE a.pid = '{$id}' ORDER BY a.id DESC ";
            $rs_log  = $this->db->query($sql_log)->result_array();
            $this->tpl->assign('logs',$rs_log);

            $factorys = $this->config->item('factorys');
            $factorys = explode("|",$factorys);
            $this->tpl->assign("factorys",$factorys);
            $this->tpl->display('factory/productprintedit.tpl');
        }


    }

    //搜寻商品信息
    public function searchProductPrint(){
        $goods_id = trim($this->input->post('goods_id'));
        $sql = "SELECT a.goods_id,a.goods_sku,a.goods_name,b.thumb_url,b.local_url FROM db_goods a ".
               " LEFT JOIN db_goods_images b ON a.goods_id = b.goods_id ".
               " WHERE a.goods_sku = '$goods_id' ";
        $result = $this->db->query($sql)->row_array();
        if($result){
            $arr_data = array('code'=>1,'local_url'=>$result['local_url'],'goods_name'=>$result['goods_name'],'msg'=>'');
            echo json_encode($arr_data);exit;
        }else{
            $arr_data = array('code'=>0,'thumb_url'=>"",'msg'=>"该商品编号不存在");
            echo json_encode($arr_data);exit;
        }
    }
    //更改订单状态
    public function editprintstatus(){
        $str_ids = trim($this->input->post('id'),",");
        $arr_ids = explode(",",$str_ids);
        $arr_print['status'] = trim($this->input->post('status'));
        if(count($arr_ids)>0){
            if($arr_print['status']==2){ //已完成
                $arr_print['finishtime']=date("Y-m-d H:i:s");
            }
            foreach($arr_ids as $k=>$v){
                $sql = "SELECT * FROM db_product_print WHERE id = '{$v}' ";
                $rs  = $this->db->query($sql)->row_array();
                if($rs['status'] != 1){ //不是生产中的订单，过滤
                    continue;
                }
                $this->db->update('db_product_print',$arr_print,"id = '".$v."'");
                if($this->db->affected_rows()){
                    if($arr_print['status']==2){ //已完成
                        //添加一条日志信息
                        $arr_log = array();
                        $arr_log['pid'] = $v;
                        $arr_log['updatetime'] = date("Y-m-d H:i:s");
                        $arr_log['updateoper'] = $_COOKIE['userid'];
                        $arr_log['descr'] = "状态由生产中修改为,已完成;";
                        $arr_log['status'] = 2;
                        $this->db->insert('db_product_printlog',$arr_log);
                    }
                    if($arr_print['status']==3){ //已作废
                        //添加一条日志信息
                        $arr_log = array();
                        $arr_log['pid'] = $v;
                        $arr_log['updatetime'] = date("Y-m-d H:i:s");
                        $arr_log['updateoper'] = $_COOKIE['userid'];
                        $arr_log['descr'] = "状态由生产中修改为,已作废;";
                        $arr_log['status'] = 3;
                        $this->db->insert('db_product_printlog',$arr_log);
                    }

                }

            }

            $data_arr = array('code'=>1,'msg'=>'修改成功!');
        }else{
            $data_arr = array('code'=>0,'msg'=>'参数错误');
        }
        echo json_encode($data_arr);exit;
    }

    function ajaxbutton(){
        $op = $this->input->get_post('op');

        if($op=="searchProductPrint"){
                $goods_id = trim($this->input->post('goods_id'));
                $sql = "SELECT a.goods_id,a.goods_sku,a.goods_name,b.thumb_url,b.local_url FROM db_goods a ".
                    " LEFT JOIN db_goods_images b ON a.goods_id = b.goods_id ".
                    " WHERE a.goods_sku = '$goods_id' ";
                $result = $this->db->query($sql)->row_array();
                if($result){
                    $arr_data = array('code'=>1,'local_url'=>$result['local_url'],'goods_name'=>$result['goods_name'],'msg'=>'');
                    echo json_encode($arr_data);exit;
                }else{
                    $arr_data = array('code'=>0,'thumb_url'=>"",'msg'=>"该商品编号不存在");
                    echo json_encode($arr_data);exit;
                }

        } 


        if($op == 'orderPrint')
        {
            $str_param = trim($this->input->get('param'),",");
            $arr_order_id = explode(",",$str_param); //order_id数组
            $arr_print = array(); //需要打印出来的数据
            foreach($arr_order_id as $k=>$v){
                //$v 为获取到的选中的id号 订单号
                $fields = "a.id,a.factory,a.create_user,a.projecttime,a.createtime,a.finishtime,a.goods_code,a.goods_name,a.print_stock,a.print_price,a.embroider_stock,a.embroider_price,a.memo,d.thumb_url,d.local_url ";
                $this->db->from('db_product_print a');
                $this->db->select($fields);
                $this->db->where('a.id',$v);
                $this->db->join('db_goods c','a.goods_code = c.goods_sku','left');
                $this->db->join('db_goods_images d','c.goods_id = d.goods_id','left');
                $this->db->group_by('a.id');
                $arr_orders = $this->db->get()->result_array();
                $arr_print[$v] = $arr_orders;
                //更新订单打印状态
                $data['print_flag'] = 1;
                $this->db->where('id',$v);
                $this->db->update('db_product_print',$data);

            }
            $this->tpl->assign("prints", $arr_print);
            $this->tpl->display('factory/productprints.tpl',0,0,1);
        }


    }





}